﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    public partial class Form1 : Form
    {
        private Utility utl = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = saveFileDialog1.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = true;

            MakeBuffer();
            utl.ReleaseMemory();

            button3.Enabled = true;
            button4.Enabled = false;
        }

        private void MakeBuffer()
        {
            soDatasetVector objDSV = null;
            soRecordset objRS = null;
            soGeoLine objLine = null;
            soGeoRegion objRegion = null;

            Utility.datasourceAlias = "route"; //textBox1.Text.Substring(textBox1.Text.IndexOf(".sdb"));
            utl = new Utility(axSuperWorkspace1, textBox1.Text);

            for (int i = 1; i <= utl.objSource.Datasets.Count; i++)
            {
                if (!utl.objSource.Datasets[i].Name.Contains(Utility.BUFFER_TITLE))
                {
                    objDSV = (soDatasetVector)utl.objSource.Datasets[i];
                    objRS = objDSV.Query("-1 < 0", true, null, "");
                    if (!(objRS.IsBOF() && objRS.IsEOF()))
                    {
                        if (!objRS.IsBOF())
                        {
                            objRS.MoveFirst();
                        }
                        for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
                        {
                            objLine = (soGeoLine)objRS.GetGeometry();
                            objRegion = objLine.Buffer(Utility.BUFFER_ALARM_DISTANCE, Utility.BUFFER_ALARM_SMOOTH);
                            Utility.routeFtWithLn[objRS.GetFieldValue(Utility.ROUTE_FIELD)] = objRegion;
                        }
                    }

                    BuildBuffer(objRS, utl.objSource.Datasets[i].Name + Utility.BUFFER_TITLE);
                }
            }

            if (utl.objSource.Modified)
            {
                Marshal.ReleaseComObject(objRegion);
                objRegion = null;
                Marshal.ReleaseComObject(objLine);
                objLine = null;
                Marshal.ReleaseComObject(objDSV);
                objDSV = null;
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        //private void MakeBufferExe(soRecordset objRS)
        //{
        //    soGeoLine objLine = null;

        //    foreach (object key in Utility.routeFtWithLn.Keys)
        //    {
        //        objLine = Utility.routeFtWithLn[key] as soGeoLine;

        //        objLine.Buffer(Utility.ALARM_DISTANCE, Utility.BUFFER_ALARM_SMOOTH);
        //    }

        //    BuildBuffer(objRS);
        //}

        private void BuildBuffer(soRecordset objRS, string datasetName)
        {
            soDatasetVector objDatasetV = null;
            soRecordset objRecords = null;

            if (!utl.objSource.IsAvailableDatasetName(datasetName))
            {
                utl.objSource.DeleteDataset(datasetName);
            }
            objDatasetV = (soDatasetVector)utl.objSource.CreateDataset(
                datasetName, seDatasetType.scdRegion, seDatasetOption.scoDefault, null);
            objRecords = objDatasetV.Query("-1 < 0", true, null, "");

            if (!objRS.IsBOF())
            {
                objRS.MoveFirst();
            }
            for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
            {
                objRecords.AddNew(
                    Utility.routeFtWithLn[objRS.GetFieldValue(Utility.ROUTE_FIELD)] as soGeometry,
                    false);
                objRecords.Update();
            }

            Marshal.ReleaseComObject(objRecords);
            objRecords = null;
            Marshal.ReleaseComObject(objDatasetV);
            objDatasetV = null;
        }
    }
}